{% extends 'base.html' %}
{% load i18n %}
{% load static %}
{% load  sizeformat %}

{% block head %}
    <link rel="stylesheet" type="text/css" href="{% static 'vms/vms_list.css' %}">
{% endblock %}
{% block title %}{% translate '虚拟机列表' %}{% endblock %}

{% block boby %}
    <div class="container-fluid">
        <div class="card">
            <div class="card-header">
                <span class="card-title"><strong><a class="btn btn-primary" href="{% url 'vms:vms-list' %}">{% translate '在线虚拟机' %} ({{ count }})</a></strong></span>
                <span class="card-title"><strong><a class="btn btn-outline-primary" href="{% url 'vms:shelve-list' %}">{% translate '已搁置' %} ({{ shelve_count }})</a></strong></span>
                <span><a class="btn btn-sm btn-primary float-right" href="{% url 'vms:vm-create' %}">
                    {% translate '创建虚拟机' %}
                </a></span>
            </div>
            <div class="card-body">
                <form role="form" class="form-horizontal" method="get" action="">
                    <div class="input-group" style="">
                        <div class="input-group-prepend">
                            <label class="input-group-text" for="id-center">{% translate '数据中心' %}</label>
                        </div>
                        <select name="center" class="custom-select" id="id-center">
                            <option value="">--</option>
                            {% for c in centers %}
                                <option value="{{ c.id }}" {% if c.id == center_id %}selected{% endif %}>
                                    {{ c.name }}
                                </option>
                            {% endfor %}
                        </select>
                        <div class="input-group-prepend">
                            <label class="input-group-text" for="id-group">{% translate '主机组' %}</label>
                        </div>
                        <select name="group" class="custom-select" id="id-group">
                            <option value="">--</option>
                            {% for g in groups %}
                                <option value="{{ g.id }}" {% if g.id == group_id|add:0 %}
                                        selected {% endif %}>{{ g.name }}</option>
                            {% endfor %}
                        </select>
                        <div class="input-group-prepend">
                            <label class="input-group-text" for="id-host">{% translate '宿主机' %}</label>
                        </div>
                        <select name="host" class="custom-select" id="id-host">
                            <option value="">--</option>
                            {% for h in hosts %}
                                <option value="{{ h.id }}" {% if h.id == host_id|add:0 %}
                                        selected {% endif %}>{{ h.ipv4 }}</option>
                            {% endfor %}
                        </select>
                        <div class="input-group-prepend">
                            <label class="input-group-text" for="id-user">{% translate '创建者' %}</label>
                        </div>
                        <select name="user" class="custom-select" id="id-user">
                            <option value="">--</option>
                            {% for u in users %}
                                <option value="{{ u.id }}" {% if u.id == user_id %}selected {% endif %}>{{ u.username }}</option>
                            {% endfor %}
                        </select>
                        <div class="input-group-prepend">
                            <label class="input-group-text">{% translate '关键字' %}</label>
                        </div>
                        <input type="text" class="form-control" name="search" value="{{ search }}"
                               placeholder="{% translate '搜 UUID、IP地址、备注' %}"/>
                        <span class="input-group-text"></span>
                        <button type="submit" class="btn btn-primary form-control">{% translate '筛选' %}</button>
                    </div>
                </form>
            </div>
            <table class="table table-hover text-center table-vm-list" style="word-wrap:break-word;word-break:break-all;">
                <thead class="thead-light">
                <tr>
                    <th></th>
                    <th>{% translate 'IP地址' %}</th>
                    <th>{% translate '创建者' %}</th>
                    <th>{% translate '镜像' %}</th>
                    <th>{% translate '宿主机' %}</th>
                    <th>CPU/MEM</th>
                    <th>{% translate '云硬盘' %}</th>
                    <th width="180px">{% translate '备注' %}</th>
                    <th>{% translate '状态' %}<button class="btn btn-sm btn-outline-success btn-update-vm-status"><i class="fa fa-sync"></i></button></th>
                    <th>{% translate '任务' %}</th>
                    <th>{% translate '操作' %}</th>
                </tr>
                </thead>
                <tbody>
                {% for vm in vms %}
                    <tr id="tr_{{ vm.hex_uuid }}">
                        <td><input type="checkbox" class="item-checkbox" value="{{ vm.hex_uuid }}" name="select_vm"></td>
                        <td title="{{ vm.hex_uuid }}">
                            <a href="{% url 'vms:vm-detail' vm_uuid=vm.hex_uuid %}"><b>{{ vm.mac_ip.ipv4 }}</b></a>

                            {% if vm.get_attach_ip_list %}
                                {% for i in vm.get_attach_ip_list %}
                                <br>
                                <b>{{ i }}</b>
                                {% endfor %}
                            {% endif %}
                        </td>
                        <td>{{ vm.user }}</td>
                        <td>{{ vm.image_name }}</td>
                        <td>{{ vm.host_ipv4 }}</td>
                        <td>
                            {{ vm.vcpu }} / {{ vm.mem|sizeformat:'GB' }}
                        </td>
                        <td>
                            {% with vm.vdisks as vdisks %}
                                {% if vdisks %}
                                    {% for v in vdisks %}
                                        {{ v.dev }} {{ v.size }}GB <br>
                                    {% endfor %}
                                {% else %}
                                        无
                                {% endif %}
                            {% endwith %}
                        </td>
                        <td class="mouse-hover">
                            <div>
                                <span>{{ vm.remarks|default_if_none:'' }}</span>
                                <span class="mouse-hover-show edit_vm_remark" vm_uuid="{{ vm.hex_uuid }}" title="{% translate '修改备注' %}">
                                    <i class="fa fa-edit"></i>
                                </span>
                            </div>
                            <div id="remarks_edit" style="display:none">
                                <textarea id="remarks">{{ vm.remarks|default_if_none:'' }}</textarea>
                                <span class="save_vm_remark" title="{% translate '保存备注' %}" vm_uuid="{{ vm.hex_uuid }}">
                                    <i class="fa fa-save"></i>
                                </span>
                            </div>
                        </td>
                        <td><span id="vm_status_{{ vm.hex_uuid }}"></span></td>
                        <td><span id="vm_task_{{ vm.hex_uuid }}"></span></td>

                        <td>
                            {% include 'vm_operations.html' %}
                        </td>
                    </tr>
                {% endfor %}
                {% if vms %}
                    <tr title="{% translate '批量操作' %}">
                        <td><input type="checkbox" data-check-target=".item-checkbox" /></td>
                        <td class="text-left" colspan="10">
                            <button class="btn btn-sm btn-primary" id="id-btn-batch-vm-start">{% translate '开机' %}</button>
                            <button class="btn btn-sm btn-warning" id="id-btn-batch-vm-shutdown">{% translate '正常关机' %}</button>
                            <button class="btn btn-sm btn-warning" id="id-btn-batch-vm-poweroff">{% translate '强制断电' %}</button>
                            <button class="btn btn-sm btn-danger" id="id-btn-batch-vm-delete">{% translate '删除' %}</button>
                            <button class="btn btn-sm btn-danger" id="id-btn-batch-vm-delete_force">{% translate '强制删除' %}
                            </button>
                        </td>
                    </tr>
                {% endif %}
                </tbody>
            </table>
            {% with page_list=page_nav.page_list previous=page_nav.previous next=page_nav.next %}
                {% if page_list %}
                    <div class="card-footer">
                        <nav aria-label="Page navigation">
                            <ul class="pagination"  style="margin:0;">
                                {% if previous %}
                                    <li class="page-item"><a class="page-link" href="?{{ previous }}" aria-label="Previous"><span
                                            aria-hidden="true">&laquo;</span></a></li>
                                {% else %}
                                    <li class="page-item disable"><span class="page-link" aria-hidden="true">&laquo;</span></li>
                                {% endif %}

                                {% for disp, query, active in page_list %}
                                    {% if active %}
                                        <li class="page-item active"><a class="page-link" href="?{{ query }}">{{ disp }}</a></li>
                                    {% else %}
                                        <li class="page-item"><a class="page-link" href="?{{ query }}">{{ disp }}</a></li>
                                    {% endif %}
                                {% endfor %}

                                {% if next %}
                                    <li class="page-item"><a class="page-link" href="?{{ next }}" aria-label="Next"><span
                                            aria-hidden="true">&raquo;</span></a></li>
                                {% else %}
                                    <li class="page-item disable"><span class="page-link" aria-hidden="true">&raquo;</span></li>
                                {% endif %}
                            </ul>
                        </nav>
                    </div>
                {% endif %}
            {% endwith %}
        </div>
    </div>
{% endblock %}

{% block script %}
    <script type="text/javascript" src="{% static 'evcloud/common.js' %}"></script>
    <script type="text/javascript" src="{% static 'vms/vms_operations.js' %}"></script>
    <script type="text/javascript" src="{% static 'vms/vms_list.js' %}"></script>
    <script type="text/javascript" src="{% static 'vms/vms_shelve_list.js' %}"></script>
{% endblock %}

